/**
 * Created by Administrator on 2017/10/20.
 */
import { observable, computed, action, autorun, toJS } from 'mobx';
import reqwest from 'reqwest';

export default class TodoListModel {

  // @observable num = [];
  @observable price = '';
  @observable palaceFloor = '';
  @observable musicPlay = true;
  @observable pocket = false;
  @observable isLight = (new Date().getHours() < 18 && new Date().getHours() > 6) + '';
  @observable endTime = false;
  @observable integer = null;
  @observable h = '0';
  @observable m = '';
  @observable s = '';
  @observable start = 0;
  @observable end = 0;
  @observable people = 0;
  @observable star = 0;
  @observable ind = -1;
  @observable message = 'false';
  @observable palaceList = [];
  @observable goodsList = [];
  @observable couponList = [];
  @observable messageList = [];
  @observable testTime = null;
  @observable scrollY = 0;
  @observable opa = 1;
  @observable quesClick = false;
  @observable music = true;
  // @observable showRes = null;

  @computed get pal() {
    return toJS(this.palaceList);
  }
  @computed get good() {
    return toJS(this.goodsList);
  }
  @computed get quanOne() {
    return toJS(this.quan1List);
  }
  @computed get mess() {
    return toJS(this.messageList);
  }
  @computed get coupon() {
    return toJS(this.couponList);
  }

  constructor() {

    // autorun(() => reqwest({
    //   url: 'http://192.168.1.103:5000/palace.do/getPalaceList',
    //   method: 'post',
    //   success: (data) => {
    //     console.log('success:', data);
    //     // this.palaceList = this.palaceList.concat(data);
    //   },
    //   error: (err) => {
    //     console.log('err: ', err);
    //   }
    // }));

    // autorun(() => reqwest({
    //   url: 'http://192.168.1.103:5000/coupon.do/getCouponList',
    //   method: 'post',
    //   success: (data) => {
    //     console.log('success:', data);
    //     this.quan = this.quan.concat(data);
    //   },
    //   error: (err) => {
    //     console.log('err: ', err);
    //   }
    // }));


    autorun( () => init(this) );

    // action(init());

    // setTimeout(reqwest({
    //   url: '//192.168.1.100:8888/mock/list.json',
    //   method: 'get',
    //   success: (data) => {
    //     console.log('success:', data);
    //     this.palaceList = this.palaceList.concat(data.data.list);
    //   },
    //   error: (err) => {
    //     console.log('err: ', err);
    //   }
    // }), 0);

  }

  @action setPalaceFloor(palaceFloor) {
    this.palaceFloor = palaceFloor;
  }

  @action quesSet(qu) {
    this.quesClick = qu;
  }
  @action musicSet() {
    this.musicPlay = !this.musicPlay;
  }

  @action setScrollY(y) {
    this.scrollY = y;
  }
  @action setOpa(opa) {
    this.opa = opa;
  }
  // @action setRes(res) {
  //   this.showRes = res;
  // }

  @action messageChange(me) {
    // this.message = this.message === 'false' ? 'true' : 'false';
    this.message = me;

  }
  @action setInd(ind) {
    this.ind = ind;
  }
  @action openPocket(is) {
    this.pocket = is;
  }
  //
  // @action numChoose(ind) {
  //   this.num.push(ind);
  // }

  @action setPrice(pri) {
    this.price = pri;
  }


  @action light() {
    this.isLight = this.isLight === 'true' ? 'false' : 'true';
  }

  @action peopleChange(people) {

    this.people = people;
  }


  @action timer() {
    // console.log(this);
    let date = new Date();
    let hour = date.getHours();
    let min = date.getMinutes();
    let sec = date.getSeconds();



    let m = min % 10;
    let mm = min / 10 % 10;

    if (m === 0 && mm === 0) {
      this.integer = true;
    } else {
      this.integer = false;
    }

    if (hour >= 0 && hour < 1) {
      this.start = 5000;
      this.end = 10000;
      this.star = 4;
    } else if (hour >= 1 && hour < 7) {
      this.start = 1000;
      this.end = 3000;
      this.star = 2;
    } else if (hour >= 7 && hour < 10) {
      this.start = 3000;
      this.end = 5000;
      this.star = 3;
    } else if (hour >= 10 && hour < 15) {
      this.start = 10000;
      this.end = 20000;
      this.star = 5;
    } else if (hour >= 15 && hour < 18) {
      this.start = 5000;
      this.end = 8000;
      this.star = 4;
    } else if (hour >= 18 && hour < 24) {
      this.start = 10000;
      this.end = 30000;
      this.star = 5;
    }


    if (sec <= 49) {
      this.s = 59 - sec;
    } else {
      this.s = '0' + (59 - sec);
    }
    if (sec === 30 || sec === 31 || sec === 32 || sec === 33 || sec === 34) {
      this.testTime = true;
    } else {
      this.testTime = false;
    }


    if (m < 10 && m > 5 ) {
      this.m = '0' + (9 - m);
      this.endTime = false;

    } else if (m < 5 && m > 0) {
      this.m = '0' + (4 - m);
      this.endTime = false;

    } else if (m == 5 || m == 0) {
      this.endTime = true;
    }

  }

  // @action sendCoupon() {
  //   const url = '//taoquan.taobao.com/coupon/unify_apply.htm?sellerId=746866993&activityId=0f9f1d7537ff44d4a6669e6797a3aa01';
  //   window.TB = {
  //     Act: {
  //       callback: (data) => {
  //         console.log('data', data);
  //       }
  //     }
  //   };
  //   reqwest({
  //     url: url + '&json=true',
  //     type: 'jsonp',
  //     jsonpCallback: 'callback',
  //     jsonpCallbackName: 'callback',
  //     success: (data) => {
  //       // alert(JSON.stringify(data));
  //       console.log('data2');
  //     },
  //     error: (err) => {
  //       console.log('err', err);
  //     }
  //   });
  // }

  // 这是异步的，在请求发回来之前就已经render了，所以取不到数据
  // componentWillMount() {
  //   reqwest({
  //     url: 'http://192.168.1.103:5000/palace.do/gteList',
  //     data: {
  //
  //     },
  //     method: 'post',
  //     success: (data) => {
  //       console.log('success:', data);
  //     },
  //     error: (err) => {
  //       console.log('err: ', err);
  //     }
  //   });
  // }

}

const init = (that) => {
  query('palace.do/getPalaceList', (data) => {
    console.log('getPalaceList', data);
    that.palaceList = data;

  });
  query('other.do/getOtherList', (data) => {
    console.log('getOtherList', data);

  });
  query( 'message.do/getMessageList', (data) => {
    console.log('getMessageList', data);
    that.messageList = data;

  });
  query( 'coupon.do/getCouponList', (data) => {
    console.log('CouponList', data);
    that.couponList = data;

  });
};

const query = (url, cb = () => {}) => reqwest({
  url: 'https://sxtest.ews.m.jaeapp.com/' + url,
  method: 'post',
  success: (data) => {
    // console.log('success:', data);
    cb(data);

  },
  error: (err) => {
    console.log('err: ', err);
  }
});
